import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'pages/address-book.dart';
import 'pages/message.dart';
import 'wigets/bottom-navitor-bar.dart';
import 'wigets/bottom-navitor-bar-item.dart';

class Tabs extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _Tabs();
  }
}

class _Tabs extends State<Tabs> with SingleTickerProviderStateMixin {
  int currentIndex = 0;
  late TabController controller;

  @override
  void initState() {
    super.initState();
    controller = TabController(length: 4, vsync: this);
    controller.addListener(() {
      setState(() {
        currentIndex = controller.index;
      });
    });
  }

  @override
  void dispose() {
    super.dispose();
    controller.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      bottomNavigationBar: BottomAppBar(
          padding: EdgeInsets.zero,
          child: BottomNavitorBar(
            currentIndex: currentIndex,
            onPressed: (index) {
              setState(() {
                currentIndex = index;
                controller.index = index;
              });
            },
            items: [
              BottomNavitorBarItem(icon: Icons.message, label: "消息"),
              BottomNavitorBarItem(icon: Icons.contact_phone, label: "通讯录"),
              BottomNavitorBarItem(icon: Icons.time_to_leave, label: "发现"),
              BottomNavitorBarItem(icon: Icons.person, label: "我的"),
            ],
          )),
      body: TabBarView(
        controller: controller,
        children: [
          Message(),
          AddressBook(),
          Center(child: Text("333")),
          Center(child: Text("444")),
        ],
      ),
    );
  }
}

class _BottomNavigationBarItem extends BottomNavigationBarItem {
  Icon icon;
  String label;
  int? badge;

  _BottomNavigationBarItem(
      {required this.icon, required this.label, this.badge})
      : super(icon: icon, label: label);

  @override
  Widget build(BuildContext context) {
    // TODO: implement build

    return Stack(
      children: [icon],
    );
  }
}
